/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package conexion;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author carlos
 */
public class Conexion {
   
    private static Conexion conexion;    
    
    private Connection conn;
    private String pathBaseDatos;
    private Statement s;
    private ResultSet rs;
    

    public Conexion(String pathBaseDatos) {
        //this.pathBaseDatos = pathBaseDatos;
        this.pathBaseDatos=getClass().getResource("/"+pathBaseDatos).getPath();
        //System.out.println(this.pathBaseDatos);
        this.CargarDriver();
    }

    public Conexion() {
    }
    
    public static void crearConexion(String path)
    {
        if(conexion==null)
        {
            conexion=new Conexion(path);
        }
    }
    
    public static Conexion getConexion()
    {
        return conexion;
    }
    
    public void CargarDriver()
    {
        try {
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
            //conn=DriverManager.getConnection("jdbc:ucanaccess://C:/ejemplo.accdb;lockmdb=false;ignorecase=true");
            conn=DriverManager.getConnection("jdbc:ucanaccess:/"+this.pathBaseDatos+";lockmdb=false;ignorecase=true");
            s = conn.createStatement();
            // ResultSet rs = s.executeQuery("SELECT * FROM cliente");
            
        } catch (SQLException ex) {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
       
    }
    
       
    public int update(String consulta)
    {
        try {
            return s.executeUpdate(consulta);
        } catch (SQLException ex) {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
        return -1;
        
    }
    
    public ResultSet select(String consulta)
    {
        try {
            return s.executeQuery(consulta);
        } catch (SQLException ex) {
            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

}
